package com.tengfei.api.course;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.main.course.domain.bo.ResBo;
import com.tengfei.main.course.domain.vo.ResVo;
import com.tengfei.main.course.service.IResService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 资源Controller
 *
 * @author tengfei
 * @date 2022-07-26
 */
@Validated
@Api(value = "资源控制器", tags = {"资源管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/course/res")
public class ResController extends BaseController {

    private final IResService iResService;

    @ApiOperation("上传资源文件")
    @SaCheckPermission("course:res:add")
    @Log(title = "上传资源文件", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public R<ResVo> uploadVideo(@RequestPart("file") MultipartFile file) {
        ResVo resVo = iResService.uploadRes(file);
        return R.ok(resVo);
    }


    /**
     * 查询资源列表
     */
    @ApiOperation("查询资源列表")
    @SaCheckPermission("course:res:list")
    @GetMapping("/list")
    public TableDataInfo<ResVo> list(ResBo bo, PageQuery pageQuery) {
        return iResService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询资源详细信息
     */
    @ApiOperation("查询资源详细信息")
    @SaCheckPermission("course:res:query")
    @GetMapping("/{resId}")
    public R<ResVo> getInfo(@ApiParam("资源id")
                                     @NotNull(message = "资源id不能为空")
                                     @PathVariable("resId") Long resId) {
        return R.ok(iResService.queryById(resId));
    }

    /**
     * 查询附件资源列表详细信息
     *
     */
    @ApiOperation("查询资源列表详细信息")
    @SaCheckPermission("course:res:query")
    @GetMapping("/listByIds")
    public R<List<ResVo>> resByResId(@ApiParam("资源id列表") @NotNull(message = "资源id列表不能为空")
                                         @RequestParam("resIds") List<Long> resIds) {
        return R.ok(iResService.resListByIds(resIds));
    }


    /**
     * 修改资源
     */
    @ApiOperation("修改资源")
    @SaCheckPermission("course:res:edit")
    @Log(title = "修改资源", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ResBo bo) {
        return toAjax(iResService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 批量删除资源
     */
    @ApiOperation("批量删除资源")
    @SaCheckPermission("course:res:remove")
    @Log(title = "批量删除资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("资源id列表")
                                       @NotNull(message = "资源id列表不能为空")
                                       @RequestParam("resIds[]") List<Long> ids) {
        return toAjax(iResService.deleteWithValidByIds(ids) ? 1 : 0);
    }

}
